var util = require('../../utils/util.js')
var d = require('date.js')
var CN_Date = require('getCNDate.js');
var CN_Week = require('getCNWeek.js');
var app = getApp()
var t = new Date();

Page({
  data: {
    monthNum: t.getMonth() + 1,
    yearNum: t.getFullYear(),
    MonthDayArray: [],
    toDate: t.getDate(),
    toMonth: t.getMonth() + 1,
    toYear: t.getFullYear(),
    fromToday: '今天',
    webName: '', // web
    javaName: '', // java
    androidName: '', // android
    iosName: '', // ios
    testName: '', // 测试
    nongliDetail: CN_Date(t.getFullYear(), t.getMonth() + 1, t.getDate()),
    week: CN_Week(t.getFullYear(), t.getMonth() + 1, t.getDate())
  },

  onShow: function() {
    this.calcMonthDayArray();
    this.dateClick()
    // d.webScheduling()
    // d.javaScheduling()
    // d.androidScheduling()
    // d.iosScheduling()
    // d.testScheduling()
  },

  dateClick: function(e) {
    var data = this.data
    var eId
    if (e) {
      eId = e.currentTarget.id;
    } else {
      eId = Number(this.data.toDate)
    }
    // console.log(eId)
    var MonArray = this.data.MonthDayArray;
    var data = this.data;
    if (eId == "") return;
    //点击效果 ，且只能选中一个日期
    //FIX 这个遍历算法可以改进
    for (var i = 0; i < MonArray.length; i++) {
      for (var j = 0; j < MonArray[i].length; j++) {
        if (typeof(MonArray[i][j]) == 'string') {
          continue;
        }
        if (MonArray[i][j].num == eId) {
          MonArray[i][j].isShowDayInfo = !MonArray[i][j].isShowDayInfo;
        }
      }
    }

    for (var i = 0; i < MonArray.length; i++) {
      for (var j = 0; j < MonArray[i].length; j++) {
        if (typeof(MonArray[i][j]) == 'string' || MonArray[i][j].num == eId) {
          continue;
        }
        MonArray[i][j].isShowDayInfo = false;
      }
    }
    // 如果不是当年当月，自动选中1号
    var notToday = (data.monthNum != t.getMonth() + 1 || data.yearNum != t.getFullYear());
    if (notToday) {
      for (var i = 0; i < MonArray[0].length; i++) {
        if (MonArray[0][i].num == 1) {
          MonArray[0][i].isShowDayInfo = true;
        }
      }
    }

    this.setData({
      MonthDayArray: MonArray,
      toYear: data.yearNum,
      toMonth: data.monthNum,
      toDate: eId,
      fromToday: d.getFromTodayDays(eId, data.monthNum - 1, data.yearNum),
      nongliDetail: CN_Date(data.yearNum, data.monthNum, eId),
      week: CN_Week(data.yearNum, data.monthNum, eId),
      webName: d.webScheduling(eId, data.monthNum - 1, data.yearNum),
      javaName: d.javaScheduling(eId, data.monthNum - 1, data.yearNum),
      androidName: d.androidScheduling(eId, data.monthNum - 1, data.yearNum),
      iosName: d.iosScheduling(eId, data.monthNum - 1, data.yearNum),
      testName: d.testScheduling(eId, data.monthNum - 1, data.yearNum)
    })
    // console.log(this.data)
  },

  // 滑动切换时间
  // monthTouch:function(e){
  //   console.log(e)
  //     var beginX = e.target.offsetLeft;
  //     var endX = e.changedTouches[0].clientX;
  //     if(beginX - endX > 125){
  //         this.nextMonth_Fn();
  //     }
  //     else if(beginX - endX < -125){
  //         this.lastMonth_Fn();
  //     }
  // },

  lickLeft: function() {
    this.lastMonth_Fn();
    this.dateClick()
  },

  lickRight: function() {
    this.nextMonth_Fn();
    this.dateClick()
  },

  nextMonth_Fn: function() {
    var n = this.data.monthNum;
    var y = this.data.yearNum;
    if (n == 12) {
      this.setData({
        monthNum: 1,
        yearNum: y + 1,
      });
    } else {
      this.setData({
        monthNum: n + 1,
      });
    }
    this.calcMonthDayArray();
  },

  lastMonth_Fn: function() {
    var n = this.data.monthNum;
    var y = this.data.yearNum;
    if (n == 1) {
      this.setData({
        monthNum: 12,
        yearNum: y - 1,
      });
    } else {
      this.setData({
        monthNum: n - 1,
      });
    }
    this.calcMonthDayArray();
  },

  calcMonthDayArray: function() {
    var data = this.data;
    // console.log(data)
    // console.log(data.webName)
    var dateArray = d.paintCalendarArray(data.monthNum, data.yearNum);

    //如果不是当年当月，自动选中1号
    var notToday = (data.monthNum != t.getMonth() + 1 || data.yearNum != t.getFullYear());
    if (notToday) {
      for (var i = 0; i < dateArray[0].length; i++) {
        if (dateArray[0][i].num == 1) {
          dateArray[0][i].isShowDayInfo = true;
        }
      }
    }
    this.setData({
      MonthDayArray: dateArray,
      toYear: notToday ? this.data.yearNum : t.getFullYear(),
      toMonth: notToday ? this.data.monthNum : t.getMonth() + 1,
      toDate: notToday ? 1 : t.getDate(),
      fromToday: notToday ? d.getFromTodayDays(1, data.monthNum - 1, data.yearNum) : '今天',
      nongliDetail: notToday ? CN_Date(data.yearNum, data.monthNum, 1) : CN_Date(t.getFullYear(), t.getMonth() + 1, t.getDate()),
      week: notToday ? CN_Week(data.yearNum, data.monthNum, 1) : CN_Week(t.getFullYear(), t.getMonth() + 1, t.getDate()),
      webName: notToday ? d.webScheduling(1, data.monthNum - 1, data.yearNum) : 'sjj',
      javaName: notToday ? d.javaScheduling(1, data.monthNum - 1, data.yearNum) : 'qg',
      androidName: notToday ? d.androidScheduling(1, data.monthNum - 1, data.yearNum) : 'cch',
      iosName: notToday ? d.iosScheduling(1, data.monthNum - 1, data.yearNum) : 'lz',
      testName: notToday ? d.testScheduling(1, data.monthNum - 1, data.yearNum) : 'sy'
    })
  }

})